<template>
  <div id="questionSubmitList">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="questionId" label="题号">
        <a-input v-model="searchParams.questionId" placeholder="请输入题号" />
      </a-form-item>
      <a-form-item field="language" label="编程语言">
        <a-select
          v-model="searchParams.language"
          :style="{ width: '120px' }"
          placeholder="请选择编程语言"
        >
          <a-option>java</a-option>
          <a-option>cpp</a-option>
          <a-option>go</a-option>
          <a-option>html</a-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSearch()">搜索</a-button>
      </a-form-item>
      <a-form-item>
        <a-button @click="refresh()">重置</a-button>
      </a-form-item>
    </a-form>

    <a-table
      style="margin-top: 30px"
      :columns="columns"
      :data="store.state?.questionSubmit?.questionSubmitPage.records"
      :pagination="{
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        showTotal: true,
        total: store.state?.questionSubmit?.questionSubmitPage.total,
      }"
      @page-change="doPageChange"
    >
      <template #creatTime="{ record }">
        {{ moment(record.createTime).format("yyyy-MM-DD") }}
      </template>
      <template #status="{ record }">
        {{ statusList[Number(record.status)] }}
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watch } from "vue";
import { QuestionSubmitControllerService } from "../../../generated";
import { Message } from "@arco-design/web-vue";
import { useRouter } from "vue-router";
import moment from "moment/moment";
import store from "@/store";

const searchParams = ref({
  pageSize: 5,
  current: 1,
  questionId: "",
  language: "",
  sortField: "createTime",
  sortOrder: "desc",
});

const statusList = ["待判题", "判题中", "成功", "失败"];

const doPageChange = (page: number) => {
  console.log("得到调用", page);
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
  getAllData();
};

const router = useRouter();

let list = ref([] as any);

const columns = [
  {
    title: "题号",
    dataIndex: "questionId",
  },
  {
    title: "编程语言",
    dataIndex: "language",
  },
  {
    title: "判题信息",
    dataIndex: "judgeInfo",
  },
  {
    title: "判题状态",
    slotName: "status",
  },
  {
    title: "创建用户id",
    dataIndex: "userId",
  },
  {
    title: "创建时间",
    slotName: "creatTime",
  },
];

const doSearch = () => {
  searchParams.value = {
    ...searchParams.value,
    current: 1,
    pageSize: 5,
  };
  getAllData();
};

// const intervalId = setInterval(() => {
//   getAllData();
// }, 3000);

const refresh = () => {
  searchParams.value = {
    pageSize: 5,
    current: 1,
    questionId: "",
    language: "",
    sortField: "createTime",
    sortOrder: "desc",
  };
  getAllData();
};

const getAllData = async () => {
  let res =
    await QuestionSubmitControllerService.listSubmitQuestionByPageUsingPost(
      searchParams.value as any
    );
  if (res.code === 0) {
    store.dispatch("questionSubmit/setQuestionList", res.data);
  } else {
    Message.error("获取失败：" + res.message);
  }
};

onMounted(async () => {
  getAllData();
  console.log("调用");
});
</script>

<style scoped>
#questionSubmitList {
  padding: 20px;
}
</style>
